open(BUF,"chr1.txt");
$num = 0;
$count = 50;
while($line=<BUF>)
{
        chomp($line);
        #print $line."\n";
        @chrarr = split(" ",$line);
        chomp($chrarr[0]);
        $chr{$chrarr[0]} = $chrarr[2];
	$chr_arr[$num] = $chrarr[0];
	$num++;
}
open(BU,">values");
open(BUFF,"human_refflat_table1");
$k =0;
$prevchr = "chr1";
$line=<BUFF>;
system("tail -1 human_refflat_table1 >lastline.txt");
open(KU,"lastline.txt");
$lastline = <KU>;
chomp($lastline);
$zzz = 2;
while($line=<BUFF> || $zzz ==1)
{
        chomp($line);
	@array = split(/\t/,$line);
	#print $array[2]."\t".$array[9]."\t".$array[10]."\n";
        #print $line."\n";
	#print "$array[2]\t$hash{$array[2]}\n";
	if($line eq $lastline)
	{
		$zzz =1;
	}
	if(($array[2] ne $prevchr || $zzz ==1)&& exists($chr{$prevchr})) 
	{
		#print "sucess $line\n";
		@order = sort {$pos[$a] <=> $pos[$b]}  0 .. $#pos;

		for($i=0;$i<@order;$i++)
		{
		       $pos1[$i] = $pos[$order[$i]];
		       $whole1[$i] = $whole[$order[$i]];
			print BU "$pos1[$i]\t$whole1[$i]\n";
		}
		#print BU "@pos1 \n @whole1\n";
                 $end = $chr{$prevchr};
                 $prv = 1;
                 $next = $prv+$count-1;
		$nu = 0;
		while($next < $end)
		{	
			$binname = $prevchr."_".$prv."_".$next;
			@value = split(/\t/,$whole[$nu]);
			if($next < $pos1[$nu])
			{
				print $binname."\t"."intergenic"."\n";
			}
			elsif($nu <@pos1)
			{
				#print "$prv\t$pos1[$nu]\t$value[3]\t$next\n";
				$wholevalue = "";
				while($pos1[$nu] <= $next && $nu <@pos1)
				{
					@value = split(/\t/,$whole1[$nu]);
					
					$wholevalue = $wholevalue.$value[4]."-$value[0]"."_";
					$nu++; 
				}
				print  $binname."\t".$wholevalue."\t"."\n";
			}
			else
			{
				print $binname."\t"."intergenic end"."\n";
			}	
			$prv = $prv+$count;
                        $next = $prv+$count-1;

		}		 	
	
		$k =0;
		@pos = ();
		@whole = ();
		@pos1 = ();
		@whole1 = ();
	}
	if(exists($chr{$array[2]}))
		{
			#print $array[9]."\t".$array[10]."\n";
			@start = split(',',$array[9]);
			@stop = split(',',$array[10]);
			#pop(@start);
			#pop(@stop);
			$prev_stop = "";
			for($i=0;$i<@stop;$i++)
			{
				#print "$prev_stop\n";
				if($prev_stop ne "")
				{
					$pprev = $prev_stop-1;
					$pos[$k] = $pprev;
					$pstart = $start[$i]-1;
					$whole[$k] = $array[0]."\t".$array[2]."\t".$pprev."\t".$pstart."\t"."intron";
					#print "$pos[$k]\n$whole[$k]\n";
					$k++;
				}
				$pos[$k] = $start[$i];
				$whole[$k] = $array[0]."\t".$array[2]."\t".$start[$i]."\t".$stop[$i]."\t"."exon";	
				#print $whole[$k]."\n";		
				#print "$pos[$k]\n$whole[$k]\n";
				$k++;
				$prev_stop = $stop[$i];
				#print "$pos[$k]\n$whole[$k]\n";
			}
		}
		else
		{
		}
		$prevchr = $array[2];
		$zzz++;
}
#@order = sort {$pos[$a] <=> $pos[$b]}  0 .. $#pos;
#for($i=0;$i<@order;$i++)
#{
#	$pos1[$i] = $pos[$order[$i]];
#	$whole1[$i] = $whole[$order[$i]];		
#}

